package com.demo.asynctaskcallback;

import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.demo.entity.SysOrderPO;
import com.demo.entity.SysUserPO;
import com.demo.mapper.OrderMapper;
import com.demo.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Component
public class WorkTaskService {
    private static final Logger log = LoggerFactory.getLogger(WorkTaskService.class);
    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    public void saveUser(SysUserPO sysUserPO) {
        // 当id = 3时, 默认抛出异常, 看order是否会回滚
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if ("3".equals(sysUserPO.getId()) || userMapper.insert(sysUserPO) == 0) {
            throw new BizException("user save fail");
        }

        log.info("user save success, po -- {}", sysUserPO);

    }

    public void saveOrder(SysOrderPO sysOrderPO) {
        if (orderMapper.insert(sysOrderPO) == 0) {
            throw new BizException("order save fail");
        }
        log.info("order save success, po -- {}", sysOrderPO);
    }
}
